using System.Collections;
using TMPro;
using UnityEngine;

[RequireComponent(typeof(CircleCollider2D))]
[RequireComponent(typeof(Rigidbody2D))]
public class Character : MonoBehaviour
{
	[SerializeField]
	private Rigidbody2D cRigidbody;

	[SerializeField]
	private Collider2D cCollider;

	[SerializeField]
	private SpriteRenderer cSprite;

	public Magnet magnet;

	public ParticleSystem boostParticle;

	public Perception perception;

	public Animator anim;

	protected Vector2 moveDirection;

	protected float moveAngle;

	protected int index;

	private bool isAlive;

	private int level;

	protected int type;

	protected int killCount;

	protected int rank;

	protected int mass;

	public Transform uiTransform;

	public TextMeshPro nameText;

	public TextMeshPro massText;

	[SerializeField]
	protected float speed;

	protected float cSpeed;

	private float maxSpeed;

	private float hitTime;

	protected bool boostEnabled;

	private bool initFrame;

	private Vector3 initPos;

	private float tempDist;

	private float angle;

	private Collectible tempColl;

	public Vector2 Position
	{
		get
		{
			return cRigidbody.position;
		}
		private set
		{
			cRigidbody.position = value;
		}
	}

	public Vector2 MoveDirection
	{
		get
		{
			return moveDirection;
		}
	}

	public int Index
	{
		get
		{
			return index;
		}
	}

	public bool IsAlive
	{
		get
		{
			return isAlive;
		}
	}

	public int Type
	{
		get
		{
			return type;
		}
	}

	public virtual int KillCount
	{
		get
		{
			return killCount;
		}
		set
		{
			killCount = value;
		}
	}

	public virtual int Rank
	{
		get
		{
			return rank;
		}
		set
		{
			rank = value;
		}
	}

	public int Mass
	{
		get
		{
			return mass;
		}
		set
		{
			SetMass(value);
		}
	}

	public float Speed
	{
		get
		{
			return speed;
		}
	}

	public bool BoostEnabled
	{
		get
		{
			return boostEnabled;
		}
		protected set
		{
			if (value != boostEnabled)
			{
				ParticleSystem.EmissionModule emission = boostParticle.emission;
				if (value)
				{
					emission.rateOverTime = 15f;
				}
				else
				{
					emission.rateOverTime = 0f;
				}
			}
			boostEnabled = value;
		}
	}

	protected bool IsHitted
	{
		get
		{
			return hitTime + Values.Instance.DieAnimDuration > Time.unscaledTime;
		}
	}

	protected virtual void SetMass(int value)
	{
		if (value >= 1)
		{
			mass = value;
		}
		else
		{
			mass = 0;
		}
		massText.text = mass.ToString();
		int num = 0;
		num = ((mass > 187) ? ((mass <= 322) ? 1 : ((mass <= 631) ? 2 : ((mass <= 973) ? 3 : ((mass <= 1453) ? 4 : ((mass > 1914) ? 6 : 5))))) : 0);
		if (level != num)
		{
			level = num;
			cSprite.sprite = GameManager.Instance.FidgetStore.fidgetTypes[type].fidgetLevels[level];
			magnet.cCollider.radius = Values.Instance.MagnetRange + (float)level * Values.Instance.MagnetGrowAmount;
		}
	}

	protected virtual void Awake()
	{
		DisablePhysics();
	}

	public virtual void Initialize(int index, Vector3 position, int mass)
	{
		anim.Play("IdleAnim");
		base.transform.parent.gameObject.SetActive(true);
		this.index = index;
		KillCount = 0;
		Rank = 0;
		cSpeed = Values.Instance.CharacterInitialSpeed;
		speed = Values.Instance.CharacterInitialSpeed;
		maxSpeed = Values.Instance.CharacterMaxSpeed;
		level = -1;
		moveDirection = GameManager.GetRandomDirection();
		initPos = position;
		initFrame = true;
		isAlive = true;
		BoostEnabled = false;
		magnet.cCollider.radius = Values.Instance.MagnetRange;
		EnablePhysics();
	}

	public void Finalize()
	{
		isAlive = false;
		StartCoroutine(KillRoutine());
	}

	protected void EnablePhysics()
	{
		cRigidbody.simulated = true;
		cCollider.enabled = true;
		magnet.EnablePhysics();
		perception.EnablePhysics();
	}

	protected void DisablePhysics()
	{
		cRigidbody.simulated = false;
		cCollider.enabled = false;
		magnet.DisablePhysics();
		perception.DisablePhysics();
	}

	protected virtual void FixedUpdate()
	{
		if (magnet.cCollider.enabled != !IsHitted)
		{
			magnet.cCollider.enabled = !IsHitted;
		}
		if (!isAlive)
		{
			return;
		}
		if (initFrame)
		{
			cRigidbody.position = initPos;
			initFrame = false;
			magnet.Collectibles.Clear();
		}
		if (!IsHitted)
		{
			if (BoostEnabled)
			{
				speed = maxSpeed;
				Mass -= Mathf.CeilToInt((float)Mass / 1000f) * Values.Instance.BoostConsumption;
			}
			else
			{
				speed = cSpeed;
			}
			Move();
		}
		magnet.cRigidbody.position = cRigidbody.position;
		perception.cRigidbody.position = cRigidbody.position;
		Rotate();
		if (magnet.Collectibles.Count <= 0)
		{
			return;
		}
		for (int i = 0; i < magnet.Collectibles.Count; i++)
		{
			tempDist = Vector3.Distance(base.transform.position, magnet.Collectibles[i].CRigidbody.position);
			if (magnet.Collectibles[i].IsAlive && tempDist < 3f)
			{
				magnet.Collectibles[i].CRigidbody.MovePosition(Vector2.MoveTowards(magnet.Collectibles[i].CRigidbody.position, cRigidbody.position, 0.05f * speed));
				if ((double)tempDist < 0.5)
				{
					Mass += magnet.Collectibles[i].Mass;
					magnet.Collectibles[i].FinalizeCollectible();
				}
			}
			else
			{
				magnet.Collectibles.RemoveAt(i);
				i--;
			}
		}
	}

	public void SetPosition(Vector2 position)
	{
		initFrame = true;
		initPos = position;
	}

	private void LateUpdate()
	{
		uiTransform.position = base.transform.position;
		cSprite.transform.rotation = base.transform.rotation;
	}

	protected virtual void Move()
	{
		cRigidbody.velocity = moveDirection * speed;
	}

	protected virtual void Rotate()
	{
		cRigidbody.angularVelocity = 6 * Mass + 20;
	}

	protected virtual void OnCollisionEnter2D(Collision2D col)
	{
		if (!isAlive)
		{
			return;
		}
		// 只有 PlayerCharacter 才触发摄像机震动
		if (IsPlayerCharacter() && col.collider.CompareTag(Values.Instance.CharacterTag))
		{
			// 触发摄像机震动
			SFXManager.Instance.PlaySFX(Const.HitSFX);
			FollowingCamera.instance.ShakeCamera(0.1f, 0.4f);
		}
		else if (IsPlayerCharacter())
		{
			FollowingCamera.instance.ShakeCamera(0.1f, 0.2f);
		}
		if (col.collider.CompareTag(Values.Instance.CharacterTag))
		{
			Character component = col.collider.GetComponent<Character>();
			if (Mass >= component.Mass)
			{
				GameManager.Instance.CharacterCollision(this, component);
			}
		}
		else if (col.collider.CompareTag(Values.Instance.WallTag))
		{
			GameManager.Instance.CharacterCollision(this, col.contacts[0]);
		}
		else if (col.collider.CompareTag(Values.Instance.BubbleTag))
		{
			GameManager.Instance.CharacterCollisionWithBouble(col.collider.GetComponent<Bubble>(), this, col.contacts[0]);
		}
	}

	public void GiveForce(Vector2 direction, float intencity)
	{
		if (!IsHitted)
		{
			hitTime = Time.unscaledTime;
			cRigidbody.velocity = Vector2.zero;
			moveDirection = direction;
			cRigidbody.AddForce(direction * intencity);
		}
	}

	protected virtual IEnumerator KillRoutine()
	{
		yield return new WaitForSeconds(Values.Instance.DieAnimDuration);
		DisablePhysics();
	}
	protected virtual bool IsPlayerCharacter()
	{
		// 默认返回 false，子类 PlayerCharacter 会重写为 true
		return false;
	}
}
